﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Client.TopLevel;

namespace PersApp
{
    public partial class PaymentForm : Form
    {
        private IManagerEmployee mManager;

        public PaymentForm(IManagerEmployee manager)
        {
            InitializeComponent();
            mManager = manager;
            initTable();

            label1.Text = "по " + DateTime.Now.ToShortDateString();
        }

        private void initTable()
        {
            employeesGridView.Columns.Add("id", "№ транзакции");
            employeesGridView.Columns.Add("time", "Время");
            employeesGridView.Columns.Add("sum", "Сумма");
            employeesGridView.Columns.Add("bank_account", "Номер счета");
            employeesGridView.Columns.Add("name", "Имя");
            employeesGridView.Columns.Add("login", "Логин");
            employeesGridView.Columns.Add("password", "Пароль");
            employeesGridView.Columns.Add("position", "Должность");
            employeesGridView.Columns.Add("role", "Полномочия");

            foreach (DataGridViewColumn column in employeesGridView.Columns)
            {
                //column.SortMode = DataGridViewColumnSortMode.NotSortable;
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            }

            refreshList();
        }

        private void refreshList()
        {
            employeesGridView.Rows.Clear();

            IDictionary<TLPayment, TLEmployee> res = mManager.GetAllPayments();
            if (res == null) return;

            foreach (TLPayment payment in res.Keys)
            {
                TLEmployee employee = res[payment];

                TLPosition position = getPositionByIndex(employee.PositionId);
                Role role           = Roles.getRoleByIndex(employee.Role);

                int rowIndex = employeesGridView.Rows.Add(
                    payment.Id,
                    payment.PayTime,
                    payment.Sum,
                    employee.BankAccountId,
                    employee.Name,
                    employee.Login,
                    employee.Password,
                    position,
                    role);

                //employeesGridView.Rows[rowIndex].Tag = employee;
            }
        }

        private TLPosition getPositionByIndex(int index)
        {
            TLPosition position = null;
            IEnumerable<TLPosition> positions = mManager.GetAllPositions();
            List<TLPosition> mPositions = new List<TLPosition>(positions);

            foreach (TLPosition p in mPositions)
            {
                if (p.Id == index)
                {
                    position = p;
                    break;
                }
            }

            return position;
        }


        private void buttonPay_Click(object sender, EventArgs e)
        {
            if (mManager.PayAll())
            {
                MessageBox.Show("Зарплата начислена.");

                refreshList();
            }
            else
            {
                MessageBox.Show("Зарплату начислить не удалось");
            }
        }
    }
}
